package HelperFinancial.DataBase;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import HelperFinancial.Class.Debito;
import android.content.Context;
import android.database.Cursor;

public class DebitoAdapter {
	
	private DebitoHelper BDHelper;
	
	private static DebitoAdapter instance;
	
	private ConfigurationAdapter configuration;
	
	private DecimalFormat fmt = new DecimalFormat("0.00");

	private DebitoAdapter(Context context) {
		this.BDHelper = new DebitoHelper(context);
		configuration  =  ConfigurationAdapter.getInstance(context);
	}
	
	public static DebitoAdapter getInstance(Context context){
		if(instance == null){
			instance = new DebitoAdapter(context);
		}
		
		return instance;
	}

	
	public static void destroy() {
		if(instance != null){
			instance.close();
		}
		instance = null;
	}
	
	private void close() {
		BDHelper.close();
	}

	public void addCount(Debito conta){
		BDHelper.insertDebit(conta);
	}
	
	public void removeAllCounts(){
		BDHelper.removeAllDebits();
	}
	
	public void removeCount(Integer Id) {
		BDHelper.removeDebit(Id);
	}
	
	public void removeParcelada(int parcelaCorrente, int idPai) {
		Cursor cr = BDHelper.getFilhosHabilitados(parcelaCorrente, idPai);
		int colId = cr.getColumnIndex("_id");
		while (cr.moveToNext()) {
			removeCount(cr.getInt(colId));
		}
	}
	
	public void updateCount(Debito conta){
		BDHelper.updateDebit(conta);
	}
	
	public void updateParcelada(Debito d, int parcelaCorrente, int idPai) {
		Cursor cr = BDHelper.getFilhosHabilitados(parcelaCorrente, idPai);
		int colId = cr.getColumnIndex("_id");
		int pCorrente = d.getParcelaCorrente();
		int contador = -1;
		while (cr.moveToNext()) {
			contador++;
			d.setParcelaCorrente(pCorrente);
			pCorrente++;
			d.setId(cr.getInt(colId));
			d.setDate(incrementaMes(d.getDate(), contador));
			updateCount(d);
		}
	}
	
	private String incrementaMes(String data, int contador) {
		if (contador == 0) return data;
		int dia = Integer.parseInt(data.split("/")[0]);
    	int mes = Integer.parseInt(data.split("/")[1]) - 1;
    	int ano = Integer.parseInt(data.split("/")[2]);
    	
    	String retorno = "";
    	
    	for (int i = 0; i < contador; i++) {
			if (mes >= 11) {
				mes = 0;
				ano++;
				retorno = (formataData(dia, mes, ano));
			}
			else if ((mes + 1 == 3 || mes + 1 == 5 || mes + 1 == 8 || mes + 1 == 10) && (dia == 31)) {
				mes++;
				retorno = (formataData(30, mes, ano));
			}
			else if (mes + 1 == 1 && dia >= 28) {
				mes++;
				retorno = (formataData(28, mes, ano));
			}
			else {
				mes++;
				retorno = (formataData(dia, mes, ano));
			}
    	}
    	return retorno;
	}
	
    private String formataData(int dia, int mes, int ano) {
    	String data = "";
    	if(dia <= 9){
            data += "0" + dia + "/";
        }else{
            data += dia + "/";
        }
        if(mes <= 8){
            data += "0" + (mes+1) + "/";
        }else{
            data += (mes+1) + "/";
        }
        data += ano;
        return data;
    }
	
	public List<Debito> getAllDebits() {
		Cursor cr = BDHelper.getAllDebits();
		List<Debito> allCounts = new ArrayList<Debito>();
		int colId = cr.getColumnIndex("_id");
		int colName = cr.getColumnIndex("name");
		int colValue = cr.getColumnIndex("value");
		int colPortions = cr.getColumnIndex("portions");
		int colType = cr.getColumnIndex("type");
		int colDate = cr.getColumnIndex("date");
		int colNotes = cr.getColumnIndex("notes");
		
		while(cr.moveToNext()){
			Debito conta =  new Debito(
					cr.getInt(colId),
					cr.getString(colName),
					cr.getDouble(colValue),
					cr.getInt(colPortions),
					cr.getString(colType),
					cr.getString(colDate),
					cr.getString(colNotes)
					);
			allCounts.add(conta);
		}
		return allCounts;
	}
	
	public Map<String, String> getDebitsByType() {
		//Cursor cr = BDHelper.getAllDebitByType();
		Cursor cr = BDHelper.getDebitsByFilter(configuration.getDateInit());
		Map<String, String> allCounts = new HashMap<String, String>();
		int colType = cr.getColumnIndex("type");
		int colVal = cr.getColumnIndex("value");
		
		while(cr.moveToNext()) {
			if (!allCounts.containsKey(cr.getString(colType)))
				allCounts.put(cr.getString(colType), fmt.format(cr.getDouble(colVal)));
			else {
				double valor = Double.parseDouble(allCounts.get(cr.getString(colType)));
				valor += cr.getDouble(colVal);
				allCounts.put(cr.getString(colType), fmt.format(valor));
			}
		}
		return allCounts;
	}
	
	public Map<String, String> getDebitsByTypeToGraphic() {
		Cursor cr = BDHelper.getAllDebitByTypeToGraphic(configuration.getDateInit());
		Map<String, String> allCounts = new HashMap<String, String>();
		int colType = cr.getColumnIndex("type");
		int colVal = cr.getColumnIndex("valueTotal");
		
		
		while(cr.moveToNext()) {
			if (!allCounts.containsKey(cr.getString(colType)))
				allCounts.put(cr.getString(colType), fmt.format(cr.getDouble(colVal)));
			else {
				double value = Double.parseDouble(allCounts.get(cr.getString(colType)));
				value += cr.getDouble(colVal);
				allCounts.put(cr.getString(colType), fmt.format(value));
			}
		}
		return allCounts;
	}
	
	public Debito getCountByName(String name){
		Cursor cr = BDHelper.getDebitByName(name);
		int colId = cr.getColumnIndex("_id");
		int colName = cr.getColumnIndex("name");
		int colValue = cr.getColumnIndex("value");
		int colPortions = cr.getColumnIndex("portions");
		int colType = cr.getColumnIndex("type");
		int colDate= cr.getColumnIndex("date");
		int colNotes= cr.getColumnIndex("notes");
		
		cr.moveToFirst();
		Debito conta =  new Debito(
				cr.getInt(colId),
				cr.getString(colName),
				cr.getDouble(colValue),
				cr.getInt(colPortions),
				cr.getString(colType),
				cr.getString(colDate),
				cr.getString(colNotes)
		);
		
		return conta;
	}
	
	public Debito getCountById(Integer id){
		Cursor cr = BDHelper.getDebitById(id);
		int colId = cr.getColumnIndex("_id");
		int colName = cr.getColumnIndex("name");
		int colValue = cr.getColumnIndex("value");
		int colPortions = cr.getColumnIndex("portions");
		int colType = cr.getColumnIndex("type");
		int colDate = cr.getColumnIndex("date");
		int colNotes = cr.getColumnIndex("notes");
		
		
		cr.moveToFirst();
		Debito conta =  new Debito(
				cr.getInt(colId),
				cr.getString(colName),
				cr.getDouble(colValue),
				cr.getInt(colPortions),
				cr.getString(colType),
				cr.getString(colDate),
				cr.getString(colNotes)
		);
		conta.setParcelaCorrente(cr.getInt(7));
		conta.setIdPai(cr.getInt(8));
		
		return conta;
	}
	
	public double getDebitsSum(){
		//Cursor cr = BDHelper.getDebitsSum();
		Cursor cr = BDHelper.getDebitsSumToGraphic(configuration.getDateInit());
		int colValue = cr.getColumnIndex("valueTotal");
		
		cr.moveToFirst();
		double value = cr.getDouble(colValue);
		
		return value;
	}
	
	public double getDebitsSumByDate(String date){
		Cursor cr = BDHelper.getDebitsSumByDate(date);
		int colValue = cr.getColumnIndex("valueTotal");
		
		cr.moveToFirst();
		double value = cr.getDouble(colValue);
		
		return value;
	}
	
	public double getDebitsSumToGraphic(){
		Cursor cr = BDHelper.getDebitsSumToGraphic(configuration.getDateInit());
		int colValue = cr.getColumnIndex("valueTotal");
		
		cr.moveToFirst();
		double value = cr.getDouble(colValue);
		
		return value;
	}
	
	public List<Debito> getDebitFilterByDatePayment(String date) {
		Cursor cr = BDHelper.getDebitsByFilter(date);
		List<Debito> allCounts = new ArrayList<Debito>();
		int colId = cr.getColumnIndex("_id");
		int colName = cr.getColumnIndex("name");
		int colValue = cr.getColumnIndex("value");
		int colPortions = cr.getColumnIndex("portions");
		int colType = cr.getColumnIndex("type");
		int colDate = cr.getColumnIndex("date");
		int colNotes= cr.getColumnIndex("notes");	
		
		while(cr.moveToNext()){
			Debito conta =  new Debito(
					cr.getInt(colId),
					cr.getString(colName),
					cr.getDouble(colValue),
					cr.getInt(colPortions),
					cr.getString(colType),
					cr.getString(colDate),
					cr.getString(colNotes)
					);
			allCounts.add(conta);
		}
		return allCounts;
	}
	
	
	public List<Debito> getDebitFilterByDateRealization(String date, String type) {
		Cursor cr = BDHelper.getDebitsByFilter(date, type);
		List<Debito> allCounts = new ArrayList<Debito>();
		int colId = cr.getColumnIndex("_id");
		int colName = cr.getColumnIndex("name");
		int colValue = cr.getColumnIndex("value");
		int colPortions = cr.getColumnIndex("portions");
		int colType = cr.getColumnIndex("type");
		int colDate = cr.getColumnIndex("date");
		int colNotes = cr.getColumnIndex("notes");
			
		while(cr.moveToNext()){
			Debito conta =  new Debito(
					cr.getInt(colId),
					cr.getString(colName),
					cr.getDouble(colValue),
					cr.getInt(colPortions),
					cr.getString(colType),
					cr.getString(colDate),
					cr.getString(colNotes)
					);
			allCounts.add(conta);
		}
		return allCounts;
	}
		
	public List<Debito> getDebitByDate(String date) {
		Cursor cr = BDHelper.getDebitsByDate(date);
		List<Debito> allCounts = new ArrayList<Debito>();
		int colId = cr.getColumnIndex("_id");
		int colName = cr.getColumnIndex("name");
		int colValue = cr.getColumnIndex("value");
		int colPortions = cr.getColumnIndex("portions");
		int colType = cr.getColumnIndex("type");
		int colDate = cr.getColumnIndex("date");
		int colNotes= cr.getColumnIndex("notes");
		
		while(cr.moveToNext()){
			Debito conta =  new Debito(
					cr.getInt(colId),
					cr.getString(colName),
					cr.getDouble(colValue),
					cr.getInt(colPortions),
					cr.getString(colType),
					cr.getString(colDate),
					cr.getString(colNotes)
					);
			allCounts.add(conta);
		}
		return allCounts;
	}

}
